<?php

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+


include('includes/InterPhoto.Core.php');


///////////////////////////////////////////////////////////////////////////// FUNCTIONS /////////////////////////

function GetAllCategories($parentid, $clevel = 0){
	global $DB, $siteicategories, $icategoryparentids;

	if($parentid != 0){
		$clevel = 2;
	}else{
		$clevel = 1;
	}

	$sReturn = '';
	$allicategoryparentids = $icategoryparentids;

	foreach($allicategoryparentids as $key => $value){
		if($parentid == $value){
			$displaycookie = ForceIncomingCookie('IPCZOOM'.COOKIE_KEY.'c_'.$key, 1);

			if($displaycookie){
				$display = 'block';
				$toggleicon = 'zoomout.gif';
			}else{
				$display = 'none';
				$toggleicon = 'zoomin.gif';
			}

			$imagescount = $siteicategories[$key]['images'];

			$sReturn .= '<div class="c_'.$clevel.'"><table cellspacing="0" cellpadding="0" width="100%">
									<tr><th class="c_h'.$clevel.'"><div>'.Iif($imagescount, '<img src="'.T_URL.'images/category.gif" border=0 align="absmiddle">&nbsp;&nbsp;') . Iif($imagescount, '<a href="'.GetUrl('index.php', 'c', $key).'">') . $siteicategories[$key]['title'] . Iif($imagescount, '</a>') . ' (' . $imagescount . ')</div>'.Iif($clevel==1, '<span><img src="'.T_URL.'images/' . $toggleicon . '" border=0 align="absmiddle" style="cursor:pointer;" onclick="ToggleCategory(\'c_'.$key.'\', this);">&nbsp;&nbsp;</span>').'</th></tr><tr><td style="padding-left:12px;padding-top:8px;"><div id="c_'.$key.'" style="display:' . $display . ';">'. Html($siteicategories[$key]['description']);

			$sReturn .= GetAllCategories($key, $clevel);

			$sReturn .= '</div></td></tr></table></div>';

		}
	}

	return $sReturn;
}

///////////////////////////////////////////////////////////////////////////// FUNCTIONS /////////////////////////


$smarty = new InterPhoto;

$siteicategories = array();
$icategoryparentids = array();

$geticategories = $DB->query("SELECT c.categoryid, c.parentid, c.title, c.description, COUNT(i.imageid) AS images FROM " . TABLE_PREFIX . "categories c LEFT JOIN " . TABLE_PREFIX . "images i ON (i.categoryid = c.categoryid AND i.actived = 1) WHERE c.actived = 1 GROUP BY c.categoryid ORDER BY c.ordernum ASC");

while($icategory = $DB->getrow($geticategories)){
	$siteicategories[$icategory['categoryid']] = $icategory;
	$icategoryparentids[$icategory['categoryid']] = $icategory['parentid'];
}


$togglesrcipt = '<script type="text/javascript">function ToggleCategory (elm, i)
{
	var getelm = document.getElementById(elm);
	if(!getelm) {
		return false;
	}else{
		if(getelm.style.display==\'none\'){
			getelm.style.display=\'block\';
			i.src = i.src.replace(\'zoomin.gif\', \'zoomout.gif\');
			setCookie(\'IPCZOOM'.COOKIE_KEY.'\'+elm, 1, 7);
		}else{
			getelm.style.display=\'none\';
			i.src = i.src.replace(\'zoomout.gif\', \'zoomin.gif\');
			setCookie(\'IPCZOOM'.COOKIE_KEY.'\'+elm, 0, 7);
		}
	}
}</script>';

$smarty->assign('togglesrcipt', $togglesrcipt);
$smarty->assign('allcategories', GetAllCategories(0));

$pagenav = '<a href="'.GetUrl('index.php').'">'.$langs['home'].'</a> '.$langs['nav'].' <a href="'.GetUrl('categories.php').'">'.$langs['categories'].'</a>';


//Always needing
$smarty->assign('pagenav', $pagenav);
$smarty->assign('userinfo', $userinfo);
$smarty->assign('pagetitle', $langs['categories'] . ' - ' .$mainsettings['siteTitle']);

$smarty->interPlay('categories.tpl');

?>